CentOS 6
Sponsored Link

Puppet - How to Use [file resource]
2014/01/26
 
This is the exmaples for file resource.
[1] It manages the configuration to keep a file like follows.
If the file is none on Puppet client, it is created.
If exists, it keeps with the specified attributes.
[root@dlp ~]#
vi /etc/puppet/manifests/site.pp
file { '/home/testfile.txt':
    ensure  => file,
    owner   => 'root',
    group   => 'root',
    mode    => 644,
    content => 'This is the puppet test file.',
}
[2] Specify the contents with variable.
[root@dlp ~]#
vi /etc/puppet/manifests/site.pp
$contents = 'This is the test Puppet manifest.
Sample contents
Test contents
'

file { '/home/testfile.txt':
    ensure  => file,
    owner   => 'root',
    group   => 'root',
    mode    => 644,
    content => "$contents",
}
[3] Specify the source file on the Puppet server as a template.
[root@dlp ~]#
vi /etc/puppet/fileserver.conf
# add at the last: specify the directory which includes template files

[extra_files]
    path /etc/puppet/files
    allow *

extra_files  ⇒  any name you like
path         ⇒  path to to the directory
allow        ⇒  clients you permit to access
                 The example above means to permit all.
                 If you specify clients, write like follows.
                 "allow 192.168.0.0/24" or "*.srv.world"

[root@dlp ~]#
vi /etc/puppet/manifests/site.pp
file { '/home/testfile.txt':
    ensure => file,
    owner  => 'root',
    group  => 'root',
    mode   => 644,
    source => 'puppet://dlp.srv.world/extra_files/test.txt',
}

[root@dlp ~]#
mkdir /etc/puppet/files

[root@dlp ~]#
echo "Puppet test file" > /etc/puppet/files/test.txt

[4] It manages the configuration to keep a link.
For example, it keeps "/home/testfile.link" from "/home/testfile.txt".
[root@dlp ~]#
vi /etc/puppet/manifests/site.pp
file { '/home/testfile.link':
    ensure => link,
    target => '/home/testfile.txt',
}
[5] It manages the configuration to keep a file does not exist. If it exists, it is deleted.
[root@dlp ~]#
vi /etc/puppet/manifests/site.pp
file { '/home/testfile.link': ensure => absent }
[6] It manages the configuration to keep a directory recursively.
The example specifies "mode" as "644" but "x" is added for directories.
Furthermore, files or directories which does not exist in source directory are deleted with the parameter "purge" or "force".
[root@dlp ~]#
vi /etc/puppet/fileserver.conf
# add at the last: specify the directory as a source

[extra_dir]
    path /etc/puppet/dirs
    allow *

[root@dlp ~]#
vi /etc/puppet/manifests/site.pp
file { '/home/testdir':
    ensure  => directory,
    recurse => true,
    purge   => true,
    force   => true,
    owner   => 'root',
    group   => 'root',
    mode    => 644,
    source  => 'puppet://dlp.srv.world/extra_dir/testdir',
}

[root@dlp ~]#
mkdir -p /etc/puppet/dirs/testdir

 
Tweet